﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Drawing;
using DockSample.Line;

namespace DockSample
{
    class OriginalLineDAO : BaseDAO
    {
        private MemoryDAO mDAO = new MemoryDAO();

        public bool AddLine(OriginalLine line, string parentFormId)
        {

            try
            {
                //           this.DeleteLine(line);
                SQLiteCommand cmd = this.GetCmd();
                StringBuilder sql = new StringBuilder("insert into OriginalLine(id,startPointX,startPointY,endPointX,endPointY,type,parentFormId,startDiagramId,endDiagramId,lineName");
                if (line.SubForm != null)
                {
                    sql.Append(",subFormId");
                }

                sql.Append(") values(@id,@startPointX,@startPointY,@endPointX,@endPointY,@type,@parentFormId,@startDiagramId,@endDiagramId,@lineName ");
                if (line.SubForm != null)
                {
                    sql.Append(",@subFormId");
                }
                sql.Append(")");
                cmd.CommandText = sql.ToString();
                cmd.Parameters.Add(new SQLiteParameter("id", line.id));
                cmd.Parameters.Add(new SQLiteParameter("startPointX", line.StartPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("startPointY", line.StartPoint.Y));
                cmd.Parameters.Add(new SQLiteParameter("endPointX", line.EndPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("endPointY", line.EndPoint.Y));

                cmd.Parameters.Add(new SQLiteParameter("type", line.GetType().ToString()));

                cmd.Parameters.Add(new SQLiteParameter("parentFormId", parentFormId));

                cmd.Parameters.Add(new SQLiteParameter("startDiagramId", line.StartDiagram.Id));
                cmd.Parameters.Add(new SQLiteParameter("endDiagramId", line.EndDiagram.Id));

                cmd.Parameters.Add(new SQLiteParameter("lineName", line.LineName));
                if (line.SubForm != null)
                {
                    cmd.Parameters.Add(new SQLiteParameter("subFormId", line.SubForm.id));
                }
                //          Console.Write(cmd.CommandText);
                cmd.ExecuteNonQuery();

                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.conn.Close();
            }
        }

        public bool DeleteLine(OriginalLine line)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete  from OriginalLine where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", line.id));
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {

                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.conn.Close();
            }
        }

        public List<OriginalLine> GetLineListByParentForm(DummyModelForm form)
        {
            List<OriginalLine> list = new List<OriginalLine>();
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "select * from OriginalLine where parentFormId=@parentFormId";
                cmd.Parameters.Add(new SQLiteParameter("parentFormId", form.id));
                SQLiteDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Point startPoint = new Point(int.Parse(reader["startPointX"].ToString()),
                        int.Parse(reader["startPointY"].ToString()));
                    Point endPoint = new Point(int.Parse(reader["endPointX"].ToString()),
                        int.Parse(reader["endPointY"].ToString()));

                    string type = this.GetSimpleType(reader["type"].ToString());
                    string id = reader["id"].ToString();
                    string startDiagramId = reader["startDiagramId"].ToString();
                    string endDiagramId = reader["endDiagramId"].ToString();
                    OriginalDiagram startDiagram = null;
                    OriginalDiagram endDiagram = null;
                    foreach (OriginalDiagram diagram in form.diagramList)
                    {


                        if (startDiagramId.Equals(diagram.Id) )
                        {
                            startDiagram = diagram;
                        }
                        else if (endDiagramId.Equals(diagram.Id) )
                        {
                            endDiagram = diagram;
                        }
                    }

                    string lineName = reader["lineName"].ToString();
                    string subFormId = reader["subFormId"].ToString();
                    string complexLineId = reader["complexLineId"].ToString();

                    OriginalLine line = null;
                    if ("DockSample.InformationFlow".Equals(type))
                    {
                        line = new InformationFlow(startPoint, endPoint, id, startDiagram, endDiagram);

                    }
                    else if ("DockSample.StateTransformFlow".Equals(type))
                    {
                        line = new StateTransformFlow(startPoint, endPoint, id, startDiagram, endDiagram);
                    }
                    else if ("DockSample.StaticComplexRelationship".Equals(type))
                    {
                        line = new StaticComplexRelationship(startPoint, endPoint, id, startDiagram, endDiagram);
                    }
                    else if ("DockSample.StaticSimpleRelationship".Equals(type))
                    {
                        line = new StaticSimpleRelationship(startPoint, endPoint, id, startDiagram, endDiagram);
                    }
                    //ComplexLine相关的
                    else if ("DockSample.Line.StartLine".Equals(type))
                    {
                        line = new StartLine(startPoint, endPoint, id, startDiagram, complexLineId);
                    }
                    else if ("DockSample.Line.EndLine".Equals(type))
                    {
                        line = new EndLine(startPoint, endPoint, id, endDiagram, complexLineId);
                    }
                    else if ("DockSample.Line.SimpleLine".Equals(type))
                    {
                        line = new SimpleLine(startPoint, endPoint, id, complexLineId);
                    }
                    else if ("DockSample.Line.ComplexLine".Equals(type))
                    {
                        line = new ComplexLine(startPoint, endPoint, id, startDiagram, endDiagram);
                    }


                    this.AddLineRelations(line, lineName, subFormId);
                    list.Add(line);
                }

                
                return this.ComposeComplexLine(list);
            }
            catch (System.Exception ex)
            {

                Console.WriteLine(ex.ToString());
                return null;
            }
            finally
            {
                this.conn.Close();
            }
        }


        private void AddLineRelations(OriginalLine line, String lineName, String subFormId)
        {
            if (lineName != null)
            {
                line.LineName = lineName;
            }
            if (subFormId != null)
            {
                DummyModelForm subForm = mDAO.findFormById(subFormId);
                line.SubForm = subForm;
            }
        }

        //构成ComplexLine对象，并且把它从form的LineList中移除
        private List<OriginalLine> ComposeComplexLine(List<OriginalLine> lineList)
        {
          List<OriginalLine> newList = new List<OriginalLine>();
            foreach (OriginalLine oline in lineList)
            {
                if (oline is StartLine)
                {
                    StartLine line =(StartLine)oline;
                    ComplexLine cline = this.GetComplexLineById(lineList, line.complexLineId);
                    cline.startLine = line;
                    cline.isComplex = true;
                   
                }
                else if (oline is EndLine)
                {
                    EndLine line = (EndLine)oline;
                    ComplexLine cline = this.GetComplexLineById(lineList, line.complexLineId);
                    cline.endLine = line;
                    cline.isComplex = true;
                   
                }
                else if (oline is SimpleLine)
                {
                    SimpleLine line = (SimpleLine)oline;
                    ComplexLine cline = this.GetComplexLineById(lineList, line.complexLineId);
                    cline.simpleLineList.Add(line);
                    cline.isComplex = true;

                }
                else
                {
                    newList.Add(oline);
                }
                
               
            }
            return newList;

        }

        private ComplexLine GetComplexLineById(List<OriginalLine> lineList,string id)
        {
            ComplexLine cline = null;
            foreach (OriginalLine oline in lineList)
            {
                if (id.Equals(oline.id))
                {
                    if (oline is ComplexLine)
                    {
                        cline = (ComplexLine)oline;
                        break;
                    }
                }
            }
            return cline;
        }

    }
}
